package com.photoeditor.demo.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;

import com.common.base.ComponentContext;
import com.photoeditor.demo.model.preference.SPDataManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/5 0005.
 */

public class AssertDataBaseManager {

    private static String dataBasePath = "/data/data/%s/database";

    private Map<String, SQLiteDatabase> databases = new HashMap<String, SQLiteDatabase>();

    private Context context = null;

    private static AssertDataBaseManager mInstance = null;

    private int newVersion;

    public static void initManager(Context context, int newVersion){
        if(mInstance == null){
            mInstance = new AssertDataBaseManager(context, newVersion);
        }
    }

    public static AssertDataBaseManager getManager(){
        initManager(ComponentContext.getContext(), HolidayUtil.DB_VERSION_HOLIDAY);
        return mInstance;
    }

    private AssertDataBaseManager(Context context, int newVersion){
        this.context = context;
        this.newVersion = newVersion;
    }

    public SQLiteDatabase getDatabase(String dbfile) {
        if(databases.get(dbfile) != null){
            return (SQLiteDatabase) databases.get(dbfile);
        }
        if(context==null)
            return null;

        String spath = getDatabaseFilepath();
        String sfile = getDatabaseFile(dbfile);

        File file = new File(sfile);
        int oldVersion = SPDataManager.getHolidayDBVersion();
        if(oldVersion != newVersion){
            if(file.exists()){
                file.delete();
            }
            file = new File(spath);
            if(!file.exists() && !file.mkdirs()){
                return null;
            }
            if(!copyAssetsToFilesystem(dbfile, sfile)){
                return null;
            }
            SPDataManager.setHolidayDBVersion(newVersion);
        }

        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(sfile, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        }catch (Exception e){

        }
        if(db != null){
            databases.put(dbfile, db);
        }
        return db;
    }

    private String getDatabaseFilepath(){
        return String.format(dataBasePath, context.getApplicationInfo().packageName);
    }

    private String getDatabaseFile(String dbfile){
        return getDatabaseFilepath()+"/"+dbfile;
    }

    private boolean copyAssetsToFilesystem(String assetsSrc, String des){
        InputStream istream = null;
        OutputStream ostream = null;
        try{
            AssetManager am = context.getAssets();
            istream = am.open(assetsSrc);
            ostream = new FileOutputStream(des);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = istream.read(buffer))>0){
                ostream.write(buffer, 0, length);
            }
            istream.close();
            ostream.close();
        }
        catch(Exception e){
            e.printStackTrace();
            try{
                if(istream!=null)
                    istream.close();
                if(ostream!=null)
                    ostream.close();
            }
            catch(Exception ee){
                ee.printStackTrace();
            }
            return false;
        }
        return true;
    }

    public boolean closeDatabase(String dbfile){
        if(databases.get(dbfile) != null){
            SQLiteDatabase db = (SQLiteDatabase) databases.get(dbfile);
            db.close();
            databases.remove(dbfile);
            return true;
        }
        return false;
    }

    static public void closeAllDatabase(){
        if(mInstance != null){
            for(int i=0; i<mInstance.databases.size(); ++i){
                if(mInstance.databases.get(i)!=null){
                    mInstance.databases.get(i).close();
                }
            }
            mInstance.databases.clear();
        }
    }

}
